/****************************************Copyright (c)**************************************************
**                                         2016-03
**
**                                
**
**
**--------------File Info-------------------------------------------------------------------------------
** File name:		    ht6x1x_uart.c
** Last modified Date:  2016-03-29
** Last Version:		v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Created by:			Evan Chen
** Created date:		2016-03-29
** Version:				v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "ht6x1x_uart.h"

void UARTx_Init(HT_UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct, IR_InitTypeDef *IR_InitStruct)
{
	uint32_t val; 

	UARTx->MODESEL = 0;
	UARTx->UARTSTA = 0x0000;
	UARTx->UARTCON = 0x0000;
	UARTx->IRCON   = 0x0000;

	UARTx->MODESEL |= MODESEL_MODE_UART;

	val = HT_FSYS_CLOCK / (2 * UART_InitStruct->UART_BaudRate);
	val++;

	UARTx->SREL = val;

	if(HT_NULL != IR_InitStruct)
	{/*CLKCTRL0 need PLL_EN set to 1*/
	  UARTx->IRCON = IR_InitStruct->IR_Lvl | IR_InitStruct->IR_Enable;
	  UARTx->IRDUTY = IR_InitStruct->IR_Duty;
	}

	val = UART_InitStruct->UART_WordLength | 
	   		UART_InitStruct->UART_StopBits |
		       	UART_InitStruct->UART_Parity;

	UARTx->UARTCON |= UARTCON_UNEG_POSITIVE | val;

	if(0 != UART_InitStruct->UART_Mode)
	{
	  UARTx->UARTCON |= UART_InitStruct->UART_Mode;
	}
}

void UARTx_DeInit(HT_UART_TypeDef* UARTx)
{
	UARTx->UARTCON = 0;

	if(HT_UART3 == UARTx || HT_UART4 == UARTx)
	{
		UARTx->ISO7816CON = 0x0000;
	}	
}

void UARTx_StructInit(UART_InitTypeDef* UART_InitStruct, IR_InitTypeDef *IR_InitStruct)
{
	UART_InitStruct->UART_BaudRate = 9600;
	UART_InitStruct->UART_WordLength = UARTCON_LENSEL_8BIT;
	UART_InitStruct->UART_StopBits = UARTCON_STOPSEL_1BIT;
	UART_InitStruct->UART_Parity = UARTCON_PARITYSEL_NONE ;
	UART_InitStruct->UART_Mode = UARTCON_RXIE_EN | UARTCON_RXEN_EN;  

	IR_InitStruct->IR_Lvl = IRCON_IRLVL_POSITIVE;
	IR_InitStruct->IR_Enable = IRCON_IRTX_EN;  
	IR_InitStruct->IR_Duty = IRDUTY_IRDUTY_1F2;  
}

void UARTx_Enable(HT_UART_TypeDef* UARTx, UART_EnableTypeDef UARTx_Type)
{
	//UARTx->UARTCON &= ~UARTCON_RXTX_MASK;
	UARTx->UARTCON |= UARTx_Type;
}

void UARTx_Disable(HT_UART_TypeDef* UARTx, UART_EnableTypeDef UARTx_Type)
{
     UARTx->UARTCON &= ~UARTx_Type;
}

uint16_t UARTx_ReadState(HT_UART_TypeDef* UARTx, UART_ReadStaDef UART_Type)
{
	uint16_t state;
  
	if(UART_RxState == UART_Type)
	{
  		state = UARTx->UARTSTA & UARTSTA_RXIF_MASK;
	   
  		if(state)	
  		{
        			UARTx->UARTSTA &= ~UARTSTA_RXIF_MASK;
  		}
	}
	else  if(UART_TxState == UART_Type)
	{
	  	state = UARTx->UARTSTA & UARTSTA_TXIF_MASK;
		   
		  if(state)	
		  {
		        UARTx->UARTSTA &= ~UARTSTA_TXIF_MASK;
		  }
	}      
	else
	{
	  	state = UARTx->UARTSTA & UARTSTA_PARITY_MASK;
		   
		  if(state)	
		  {
		        UARTx->UARTSTA &= ~UARTSTA_PARITY_MASK;
		  }
	}

   	return state;
}

void UART_ISO7816_Init(HT_UART_TypeDef* UARTx, ISO7816_InitTypeDef* ISO7816_InitStruct)
{
	uint32_t val; 

	UARTx->MODESEL = 0;
	UARTx->ISO7816STA = 0x0000;
	UARTx->ISO7816CON = 0x0000;
	UARTx->MODESEL |= MODESEL_MODE_7816;

	val = HT_FSYS_CLOCK / (2 * ISO7816_InitStruct->ISO7816_BaudRate);
	val++;

	UARTx->SREL = val;

	UARTx->ISO7816CON |= ISO7816_InitStruct->ISO7816_AutoRxTx | 
	   ISO7816_InitStruct->ISO7816_AutoRxTxRepeat |
	   ISO7816_InitStruct->ISO7816_AckLen |
	   ISO7816_InitStruct->ISO7816_Parity;

	if(0 != ISO7816_InitStruct->ISO7816_Mode)
	{
	  UARTx->ISO7816CON |= ISO7816_InitStruct->ISO7816_Mode;
	}
}

void UART_ISO7816_StructInit(ISO7816_InitTypeDef* ISO7816_InitStruct)
{
	ISO7816_InitStruct->ISO7816_BaudRate = 9600;
	ISO7816_InitStruct->ISO7816_AutoRxTx = ISO7816CON_AUTORXEN_EN | ISO7816CON_AUTOTXEN_EN;
	ISO7816_InitStruct->ISO7816_AutoRxTxRepeat = ISO7816CON_REPTR_2;
	ISO7816_InitStruct->ISO7816_AckLen = ISO7816CON_ACKLEN_1BIT;
	ISO7816_InitStruct->ISO7816_Parity = ISO7816CON_PARITY_EVEN ;
	ISO7816_InitStruct->ISO7816_Mode = ISO7816CON_RXIE_EN;  
}

void UART_ISO7816_Enable(HT_UART_TypeDef* UARTx, ISO7816_EnableTypeDef ISO7816_Type)
{
    UARTx->ISO7816CON &= ~ISO7816CON_RXTXPRD_MASK;
    UARTx->ISO7816CON |= ISO7816_Type;
}

void UART_ISO7816_Disable(HT_UART_TypeDef* UARTx, ISO7816_EnableTypeDef ISO7816_Type)
{
    UARTx->ISO7816CON &= ~ISO7816_Type;
}

uint16_t UART_ISO7816_ReadState(HT_UART_TypeDef* UARTx, ISO7816_ReadStaDef ISO7816_Type)
{
	uint16_t state;
	  
	if(ISO7816_RxState == ISO7816_Type)
	{
		state = UARTx->ISO7816STA & ISO7816STA_RXIF_MASK;
		   
		if(state)	
		{
	        UARTx->ISO7816STA &= ~ISO7816STA_RXIF_MASK;
		}
	}     
	else  if(ISO7816_TxState == ISO7816_Type)
	{
		state = UARTx->ISO7816STA & ISO7816STA_TXIF_MASK;
		   
		if(state)	
		{
	        UARTx->ISO7816STA &= ~ISO7816STA_TXIF_MASK;
		}
	}      
	else if(ISO7816_RxErrState == ISO7816_Type)
	{
		state = UARTx->ISO7816STA & ISO7816STA_RXERRSTAT_MASK;
		   
		if(state)	
		{
	        UARTx->ISO7816STA &= ~ISO7816STA_RXERRSTAT_MASK;
		}
	} 
	else  if(ISO7816_TxErrState == ISO7816_Type)
	{
		state = UARTx->ISO7816STA & ISO7816STA_TXERRSTAT_MASK;
		   
		if(state)	
		{
	        UARTx->ISO7816STA &= ~ISO7816STA_TXERRSTAT_MASK;
		}
	} 
	else
	{
		state = UARTx->ISO7816STA & ISO7816STA_PRDIF_MASK;
		   
		if(state)	
		{
	        UARTx->ISO7816STA &= ~ISO7816STA_PRDIF_MASK;
		}
	} 

	return state;
}

void UART_SendData(HT_UART_TypeDef* USARTx, uint16_t Data)
{
	USARTx->SBUF = Data;
}

uint16_t UART_ReceiveData(HT_UART_TypeDef* USARTx)
{
	return USARTx->SBUF;
}

